package com.chixing.mapper;

import com.chixing.entity.RoomInventory;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chixing.entity.VO.RoomCountVO;
import com.chixing.utils.Result;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author smith
 * @since 2025-09-30
 */
@Mapper
public interface RoomInventoryMapper extends BaseMapper<RoomInventory> {
    //查修某个时间段，该酒店的，每个房型的而剩余数量,存到redis中
     List<RoomCountVO> selectHotelDateRoomTypeCount(@Param("hotelId") Long hotelId,
                                                          @Param("checkIn") Date checkIn,
                                                          @Param("checkOut") Date checkOut);
    //下单时锁定库存
    int increaseLockedCount(@Param("hotelId") Long hotelId,
                            @Param("roomTypeId") Long roomTypeId,
                            @Param("invDate") Date invDate,
                            @Param("checkIn") Date checkIn,
                            @Param("checkOut") Date checkOut);
    //支付成功后locked_count-1,booked_count+1
    int addBookCount(@Param("hotelId") Long hotelId,
                            @Param("roomTypeId") Long roomTypeId,
//                            @Param("invDate") Date invDate,
                            @Param("checkIn") LocalDate checkIn,
                            @Param("checkOut") LocalDate checkOut);


    /**
     * 取消订单时回退库存锁（locked_count - 1）
     * @param hotelId 酒店ID
     * @param roomtypeId 房型ID
     * @param checkIn 入住日期
     * @param checkOut 退房日期
     * @return 影响行数（需与入住天数一致）
     */
    int decreaseLockedCount(@Param("hotelId") Long hotelId,
                            @Param("roomtypeId") Long roomtypeId,
                            @Param("checkIn") LocalDate checkIn,
                            @Param("checkOut") LocalDate checkOut);

    LocalDate findMaxInvDate();

    void insertBatchSomeColumn(@Param("list") List<RoomInventory> list);

    void archiveOldInventory();

    void deleteArchivedInventory();
    //做延迟退房的查询房型剩余数量
    Integer selectRoomTypeRoomCount(@Param("hotelId") Long hotelId,
                              @Param("roomTypeId")Long roomTypeId,
                              @Param("checkOut")LocalDate checkOut);

    /**
     * 单日释放booked_count
     */
    int releaseBookedInventory(@Param("roomTypeId") Long roomTypeId,
                               @Param("invDate") Long invDate,
                               @Param("roomCount") Long roomCount);

    /**
     * 批量（多天）释放 booked_count，使用 IN (...) 一次更新多条记录
     */
    int releaseBookedInventoryBatch(@Param("roomTypeId") Long roomTypeId,
                                    @Param("dates") List<LocalDate> dates,
                                    @Param("roomCount") Integer roomCount);

}
